package com.vinxin.app.model

import com.vinxin.app.exception.ResponseCodeEnum

class ResponseModel<T>(
    val body: T? = null,
    val code: Int? = null,
    val count: Int? = null,
    val isSuccess: Boolean = false,
    val message: String? = null
) {
    companion object {
        @JvmStatic
        fun <T> ok(body: T): ResponseModel<T> {
            return response(true, ResponseCodeEnum.SUCCESS, body, null)
        }

        @JvmStatic
        fun <T> ok(body: T, count: Int): ResponseModel<T> {
            return response(true, ResponseCodeEnum.SUCCESS, body, count)
        }

        @JvmStatic
        fun <T> error(responseCodeEnum: ResponseCodeEnum): ResponseModel<T> {
            return response(false, responseCodeEnum, null, null)
        }

        @JvmStatic
        fun <T> error(responseCodeEnum: ResponseCodeEnum, body: T): ResponseModel<T> {
            return response(false, responseCodeEnum, body, null)
        }

        private fun <T> response(
            isSuccess: Boolean,
            responseCodeEnum: ResponseCodeEnum,
            body: T?,
            count: Int?
        ): ResponseModel<T> {
            return ResponseModel(body, responseCodeEnum.code, count, isSuccess, responseCodeEnum.message)
        }
    }
}